package elijah.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.Locale;

import elijah.base.R;
import elijah.base.utils.DensityUtils;

public class CircleProgressView extends View {
    private Paint paint;
    private TextPaint textPaint;
    private int width, height;
    private int radius;
    private final RectF bounds = new RectF();
    private float rate = 0.f;

    private boolean showLabel;
    private String labelFormat;
    private int labelTextColor;
    private float labelTextSize;

    private int normalColor;
    private int activeColor;
    private float thickness;


    public CircleProgressView(Context context) {
        this(context, null);
    }


    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
            showLabel = typedArray.getBoolean(R.styleable.CircleProgressView_showLabel, true);
            labelFormat = typedArray.getString(R.styleable.CircleProgressView_labelFormat);
            if (TextUtils.isEmpty(labelFormat)) {
                labelFormat = "%.1f%%";
            }
            labelTextColor = typedArray.getColor(R.styleable.CircleProgressView_labelTextColor, Color.WHITE);
            labelTextSize = typedArray.getDimension(R.styleable.CircleProgressView_labelTextSize, 16);

            normalColor = typedArray.getColor(R.styleable.CircleProgressView_circleNormalColor, 0xff05608d);
            activeColor = typedArray.getColor(R.styleable.CircleProgressView_circleActiveColor, 0xff00ff00);
            thickness = typedArray.getDimension(R.styleable.CircleProgressView_thickness, 8);

            typedArray.recycle();
        }
        init();


    }

    public CircleProgressView setRate(float rate) {
        this.rate = rate;
        postInvalidate();
        return this;
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (showLabel) {
            initTextPaint();
        }

    }

    private void initTextPaint() {
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(labelTextColor);
        textPaint.setTextSize(labelTextSize);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setFakeBoldText(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width / 2f, height / 2f);
        if (normalColor != Color.TRANSPARENT) {
            paint.setColor(normalColor);
            paint.setStrokeWidth(thickness);
            paint.setStyle(Paint.Style.STROKE);
            bounds.set(-radius / 2f, -radius / 2f, radius / 2f, radius / 2f);
            canvas.drawCircle(bounds.centerX(), bounds.centerY(), radius / 2f, paint);
        }
        paint.setColor(activeColor);
        bounds.set(bounds.centerX() - radius / 2f, bounds.centerY() - radius / 2f, bounds.centerX() + radius / 2f, bounds.centerY() + radius / 2f);
        canvas.drawArc(bounds, -90, 360 * Math.min(1, rate), false, paint);
        if (showLabel) {
            drawLabel(canvas);
        }

    }

    private void drawLabel(Canvas canvas) {
        String text = String.format(Locale.CHINA, labelFormat, rate * 100);
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = bounds.centerY() + distance;
        canvas.drawText(text, bounds.centerX(), baseline, textPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        radius = (int) (Math.min(width, height) - Math.max(getPaddingLeft() + getPaddingRight(), getPaddingTop() + getPaddingBottom()) - thickness);
        setMeasuredDimension(width, height);
    }

    public CircleProgressView setColor(int activeColor) {
        this.activeColor = activeColor;
        return this;
    }

    public CircleProgressView setActiveColor(int activeColor) {
        this.activeColor = activeColor;
        return this;
    }

    public CircleProgressView setNormalColor(int normalColor) {
        this.normalColor = normalColor;
        return this;
    }
}
